package com.gxa.controller.points;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.ImageUtil;
import com.gxa.common.utils.Result;
import com.gxa.entity.points.ExchangeRecord;
import com.gxa.entity.points.Gift;
import com.gxa.service.points.GiftService;
import com.gxa.service.points.IntegrationService;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:43:53
 */

@Api(tags = "礼品管理模块")
@RestController
@RequestMapping("/gift")
public class GiftController {
    @Data
    @AllArgsConstructor
    class K {
        String GiftName;
        Integer count;
        Integer pageNumber;
    }

    @Autowired
    private GiftService giftService;

    @ApiOperation(value = "查询礼品接口")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/gifts")
    public Result<PageInfo> queryAll(Integer pagenum,Integer count) {
        try {
            Page<Gift> page1 = PageHelper.startPage(pagenum, count);
            List<Gift> list = this.giftService.queryAll();
            PageInfo<Gift> pageInfo = new PageInfo<Gift>(list);
            return Result.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            Result r = Result.failed("更新失败");
            return r;
        }
    }

    @ApiOperation(value = "新增礼品")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/add")
    public Result add(@RequestParam(value = "upload" ,required = false) MultipartFile upload, Gift gift) {
        try {
            this.giftService.add(gift,upload);
            Result r = Result.success("添加成功");
            return r;
        } catch (Exception e) {
            e.printStackTrace();
            Result r = Result.failed("添加失败");
            return r;
        }

    }

    @ApiOperation(value = "条件查询数据")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/gift")
    public Result queryByName(@RequestParam("name") String name,@RequestParam("pagenum") Integer pagenum, @RequestParam("count") Integer count) {
        try {
            Page<Gift> page1 = PageHelper.startPage(pagenum,count);
            List<Gift> giftList = this.giftService.queryByName(name);
            PageInfo<Gift> pageInfo = new PageInfo<Gift>(giftList);
            return Result.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("查询失败");
        }
    }

        @ApiOperation(value = "根据Id查询数据")
        @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
        @PostMapping("/gift/{id}")
        public Result queryById(@PathVariable("id") Integer id) {
            try {
                Gift gift = this.giftService.queryId(id);
                return Result.success(gift);
            }catch (Exception e){
                e.printStackTrace();
                return Result.failed("查询失败");
            }


        }

    @ApiOperation(value = "礼品的编辑")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PostMapping("/edit")
    public Result edit(@RequestParam(value = "upload" ,required = false) MultipartFile upload, Gift gift) {
        try {
            this.giftService.update(gift,upload);
            Result r = Result.success("更新成功");
            return r;
        } catch (Exception e) {
            Result r = Result.failed("更新失败");
            return r;
        }
    }

    @ApiOperation(value = "礼品删除")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @DeleteMapping("/del/{id}")
    public Result delete(@ApiParam("删除的数据id") @PathVariable("id") Integer id) {
        try {
            this.giftService.delete(id);
            Result r = Result.success("删除成功");
            return r;
        } catch (Exception e) {
            Result r = Result.failed("删除失败");
            return r;
        }
    }

    @ApiOperation(value = "礼品上下架")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @PutMapping("/shelve")
    public Result updateByid(Integer id, Integer status){
        try{
            this.giftService.updateByid(id,status);
            return Result.success("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("更新失败");
        }
    }


//    @ApiOperation(value = "图片上传")
//    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
//    @PostMapping(value = "/uploadfile")
//    public String fileUpload(@RequestParam(value = "upload" ,required = false) MultipartFile upload) throws Exception {
//        System.out.println(upload.getContentType());
//        System.out.println("来上传文件了");
//        System.out.println(upload.getName());
//        if (upload.isEmpty() || upload == null) {
//            System.out.println("上传失败");
//            return "fail";
//        } else {
//            String address = ImageUtil.upload(upload);
//            return address;
//        }
//    }



}
